package com.dykj.fireFighting.web.mq;

import com.alibaba.fastjson.JSON;
import com.dykj.fireFighting.api.elasticsearch.devHisSXin.entity.EsDevHisSXin;
import com.dykj.fireFighting.api.elasticsearch.devHisSXin.service.EsDevHisSXinService;
import com.dykj.fireFighting.common.starter.redisson.service.RedisService;
import com.dykj.fireFighting.web.entity.DevHisSXin;
import com.dykj.fireFighting.web.message.InsertDevHisSXinMessage;
import com.dykj.fireFighting.web.service.DevHisSXinService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service
@RocketMQMessageListener(
        topic = InsertDevHisSXinMessage.TOPIC,
        consumerGroup = "dy-consumer-group-" + InsertDevHisSXinMessage.TOPIC
)
public class InsertDevHisSXinConsumer implements RocketMQListener<InsertDevHisSXinMessage> {
    public static final Logger LOGGER = LoggerFactory.getLogger(InsertDevHisZcConsumer.class);
    @Autowired
    private RedisService redisService;
    @Autowired
    private DevHisSXinService devHisSXinService;
    @DubboReference
    private EsDevHisSXinService esDevHisSXinService;
    @Override
    public void onMessage(InsertDevHisSXinMessage insertDevHisSXinMessage) {
        DevHisSXin devHisSXin =  insertDevHisSXinMessage.getDevHisSXin();
        LOGGER.info("===========插入四信数据到mysql==============");
        devHisSXinService.insertDevHisSXin(devHisSXin);
        LOGGER.info("===========插入四信数据到ES==============");
        EsDevHisSXin esDevHisSXin = new EsDevHisSXin();
        BeanUtils.copyProperties(devHisSXin, esDevHisSXin);
        esDevHisSXin = esDevHisSXinService.importAll(esDevHisSXin);
        LOGGER.info("===========插入EsDevHis数据到ES:=============" + JSON.toJSONString(esDevHisSXin));

        redisService.set(devHisSXin.getId(), JSON.toJSONString(devHisSXin), 3600L);
    }
}
